import * as THREE from 'three'
import Sizes from './Sizes';
import Camera from './Camera'
import Renderer from './Renderer';
import Time from './Time';
import Cube from './Cube';

// 单例 由于THREE.js中各种类互有引用 因此创建一个统一的类来管理它们
export default class Experience {
    static instance;
    constructor(canvas, outBox) {
        
        if(Experience.instance) return Experience.instance;
        Experience.instance = this;
        this.outBox = outBox;
        this.canvas = canvas;
        this.scene = new THREE.Scene()
        this.sizes = new Sizes();
        this.camera = new Camera();
        this.time = new Time();
        this.renderer = new Renderer();

        this.cube = new Cube();

        this.time.on('update', ()=> {
            this.update();
        })
        this.sizes.on('resize', ()=> {
            this.resize();
        })
    }
    update(){
        this.camera.update();
        this.renderer.update();
        
    }
    resize(){
        this.camera.resize();
        this.renderer.resize();
    }
    destroy(){
        Experience.instance = null;
    }
}